package org.leaf.green.b2b

import org.leaf.green.admin.RecordType
import org.springframework.transaction.annotation.*
import org.codehaus.groovy.grails.web.metaclass.BindDynamicMethod
import org.hibernate.FetchMode as FM


class MarketPriceActualService {

    static transactional = true

    @Transactional(readOnly = true)
    def list(Object params) {
        def result = MarketPriceActual.withCriteria{
            if(params?.max) maxResults(params.int('max'))
			if(params?.offset) firstResult(params.int('offset'))
			if(params?.sort && params?.order) order(params?.sort, params?.order)
            order("id", "asc")
        }
        return result
    }

    @Transactional(readOnly = true)
    def count() {
       def result =  MarketPriceActual.withCriteria{
            count("id")
       }
       return result
    }

    //获取区域基准价格
    @Transactional(readOnly = true)
    def todayMarketPriceApproval() {
        def result =  MarketPriceApprovalDetail.withCriteria{
            createAlias 'marketPriceApproval', 'approval'
            projections{
                property('product')
                property('regionPrice')
            }
            fetchMode "marketPriceApproval", FM.JOIN
            order("serialNumber", "asc")
        }
        return result
    }

    def init(Object params) {
        def marketPriceActualInstance = new MarketPriceActual()
            marketPriceActualInstance.approved = false

        //取出所有产品的品名
        def flstProduct = Product.withCriteria{
            projections{
                groupProperty("name")
            }
            order("name", "asc")
        }
        //取出所有门店列表
        def retailerInstanceList = Retailer.withCriteria{
            createAlias 'region', 'reg'
            projections{
                property('id')
                //property('serialNumber')
                //property('name')
                //property('reg.id')
                //property('reg.serialNumber')
                //property('reg.name')
            }
            fetchMode "region", FM.JOIN
            order("serialNumber", "asc")
        }

        def todayProductRegionApprovalPrice = this.todayMarketPriceApproval()
        def fmapTodayProductRegionApprovalPrice = [:]

            todayProductRegionApprovalPrice.each{
                fmapTodayProductRegionApprovalPrice.put(it[0], it[1])
            }

        def begin_serialNumber = 0
        flstProduct.each{ product ->
            begin_serialNumber += 1
            def detail = new MarketPriceActualDetail(
                  serialNumber : String.format('%03d', begin_serialNumber)
                , product : product
                , regionPrice : fmapTodayProductRegionApprovalPrice.get(product) ?: 0
            )
            def detail_begin_serialNumber = 0
            retailerInstanceList.each{retailer ->
                detail_begin_serialNumber += 1

                def detailDetail = new MarketPriceActualDetailDetail(
                      serialNumber : String.format('%03d', detail_begin_serialNumber)
                    , retailer : retailer
                    , lowPrice : fmapTodayProductRegionApprovalPrice.get(product) ?: 0
                    , highPrice : fmapTodayProductRegionApprovalPrice.get(product) ?: 0
                )
                                
                //set retailer
                BindDynamicMethod mybind = new BindDynamicMethod()
                def mymap = ['retailer.id' : retailer]
                def myargs =  [detailDetail, mymap]
                mybind.invoke(detailDetail, 'bind', (Object[]) myargs)
 
                detail.addToDetails(detailDetail)
            }
            marketPriceActualInstance.addToDetails(detail)
        }
        return marketPriceActualInstance
    }
}
